package io.renren.modules.generator.controller;

import com.baomidou.mybatisplus.mapper.Condition;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;
import io.renren.common.utils.ShiroUtils;
import io.renren.modules.app.enums.OutOrderStatusEnum;
import io.renren.modules.app.request.intoorder.SubmitInOrderItemReq;
import io.renren.modules.app.request.intoorder.SubmitInOrderReq;
import io.renren.modules.generator.entity.InOrderEntity;
import io.renren.modules.generator.entity.InOrderItemEntity;
import io.renren.modules.generator.entity.PositionEntity;
import io.renren.modules.generator.service.InOrderItemService;
import io.renren.modules.generator.service.InOrderService;
import io.renren.modules.generator.service.PositionService;
import io.renren.modules.generator.service.StockService;
import io.renren.modules.generator.vo.InOrderListVo;
import io.renren.modules.sys.entity.SysUserEntity;
import org.apache.commons.lang.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.*;


/**
 * 入库单
 *
 * @author chenshun
 * @email sunlightcs@gmail.com
 * @date 2018-12-11 00:13:08
 */
@RestController
@RequestMapping("generator/inorder")
public class InOrderController {
    @Autowired
    private InOrderService inOrderService;


    @Autowired
    StockService stockService;

    @Autowired
    PositionService positionService;

    @Autowired
    InOrderItemService inOrderItemService;

    @PostMapping("submitInOrder")
    @RequiresPermissions("generator:inorder:submitorder")
    public R submitInOrder(@RequestBody SubmitInOrderReq req) {

        SysUserEntity user = ShiroUtils.getUserEntity();

        InOrderEntity order = new InOrderEntity();
        order.setOrderNo(req.getOrderNo());
        order.setStatus(OutOrderStatusEnum.初始化.getValue());
        order.setCreateTime(new Date());
        order.setCreateUser(user.getUsername());
        if (inOrderService.insert(order)) {
            List<InOrderItemEntity> orderItems = new ArrayList<>();
            List<SubmitInOrderItemReq> reqOrderItems = req.getOrderItems();
            reqOrderItems.stream().forEach(x -> {
                InOrderItemEntity vo = new InOrderItemEntity();
                BeanUtils.copyProperties(x, vo);
                vo.setItemGuid(x.getGuid());
                vo.setBatchNo(x.getBatchNo());
                vo.setQuantity(x.getQuantity());
                vo.setOrderNo(req.getOrderNo());
                vo.setProportion(100.0);
                PositionEntity pos = positionService.selectOne(Condition.create().eq("ps_code", x.getPsCode()));
                vo.setAreaCode(pos == null ? "" : pos.getAreaCode());
                orderItems.add(vo);
            });
            if (orderItems.size() > 0) {
                inOrderItemService.insertBatch(orderItems);
            }
        }
        return R.ok();
    }

    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("generator:inorder:list")
    public R list(@RequestParam Map<String, Object> params) {
        if (params.containsKey("key") && StringUtils.isNotBlank(params.get("key").toString())) {
            params.put("orderNo", params.get("key"));
        }
        List<InOrderListVo> response = new ArrayList<>();
        PageUtils page = inOrderService.queryPage(params);
        List<InOrderEntity> datas = (List<InOrderEntity>) page.getList();
        datas.stream().forEach(x -> {
            InOrderListVo vo = new InOrderListVo();
            if (x.getFinishTime().getTime() < 100000) {
                x.setFinishTime(null);
            }
            BeanUtils.copyProperties(x, vo);
            response.add(vo);
        });
        page.setList(response);
        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    @RequiresPermissions("generator:inorder:info")
    public R info(@PathVariable("id") Long id) {
        InOrderEntity inOrder = inOrderService.selectById(id);

        return R.ok().put("inOrder", inOrder);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("generator:inorder:save")
    public R save(@RequestBody InOrderEntity inOrder) {
        inOrderService.insert(inOrder);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("generator:inorder:update")
    public R update(@RequestBody InOrderEntity inOrder) {
        inOrderService.updateById(inOrder);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("generator:inorder:delete")
    public R delete(@RequestBody Long[] ids) {
        inOrderService.deleteBatchIds(Arrays.asList(ids));

        return R.ok();
    }

}
